(() => {
  let ele = {};
  ele.cabinet = $("#cabinet");
  ele.yijian = $("#yijian");
  ele.modal_tips2 = $("#modal-tips2");
  ele.close_tips2 = $("#close-tips2");
  ele.recieveimg = $("#recieveimg");
  ele.recieveimg2 = $("#recieveimg2");
  ele.tipsbtn = $("#tipsbtn");
  ele.mingzi = $("#mingzi");
  ele.floimg = $("#floimg");
  ele.baoxiang = $("#baoxiang");
  ele.getsuccessList = $("#getsuccessList");
  ele.xhao = $("#xhao");
  ele.catimg2 = $("#catimg2");
  ele.modal_jilu = $("#modal-jilu");
  ele.more = $("#more");
  ele.empty = $("#empty");
  ele.list_jlu = $("#list_jlu");
  ele.meiyou = $("#meiyou");
  ele.close_getsuccess = $("#close-getsuccess");
  ele.modal_getsuccess = $("#modal-getsuccess");
  let list = [];
  let page = 1;
  let qgjlList = [];
  // closeMdl(ele.recieveimg);
  //关闭 恭喜获得
  ele.close_tips2.onclick = () => {
    closeMdl(ele.modal_tips2);
  };
  ele.tipsbtn.onclick = () => {
    closeMdl(ele.modal_tips2);
  };
  // !点击确定关闭一键领取
  ele.close_getsuccess.onclick = () => {
    closeMdl(ele.modal_getsuccess);
  };
  // 点击关闭领取记录
  ele.xhao.onclick = () => {
    page = 1;
    qgjlList = [];
    closeMdl(ele.modal_jilu);
  };
  // 点击打开领取记录
  ele.catimg2.onclick = () => {
    qiegejl();
    openMdl(ele.modal_jilu);
  };
  // 请求数据
  asyncList();
  function asyncList() {
    let token = getCookie("token");
    ajax({
      method: "get",
      url: API_PATH.JIANGLI,
      header: {
        Authorization: token,
      },
      success: (res) => {
        list = res.data;
        initList();
      },
      error: (err) => {
        // openTips(err.msg);
      },
    });
  }

  // !渲染数据
  function initList() {
    let newList = list
      .map((item, index) => {
        return `
      <div class="box">
      <div class="cab1 radius3 font14 txtShadowRed">期数：${
        item[0].qi_num
      }</div>
    
       <div class="thingbox thingbox2" >
       <div style="display:${item[0].state != 10 ? "block" : "none"};">
      <div id="xianshi"  style="display:flex;align-items: baseline;" data-num="${
        index + 1
      }">
      
      
        <div class="thingLeft" id="baoxiang" data-num="${
          item.num
        }" data-type="${item[0].type}" data-h_id="${item.h_id}">
          <div class="thing">
            <div class="thingin thingcoffee">
              <img src="http://img.qxgs.cc/tiantianlaishouzu/img/reward/6.png" alt="" />
              <span>x${item[0].get_money}</span>
            </div>
            <img
              class="thingdimg"
              src="http://img.qxgs.cc/tiantianlaishouzu/img/reward/dizuo.png"
              alt=""
            />
          </div>
          <div  class="thing">
            <div class="thingin thingorange">
              <img src="http://img.qxgs.cc/tiantianlaishouzu/img/reward/7.png" alt="" />
              <span>x50</span>
            </div>
            <img
              class="thingdimg"
              src="http://img.qxgs.cc/tiantianlaishouzu/img/reward/dizuo.png"
              alt=""
            />
          </div>
        </div>
        </div>
        </div>
        <div class="thingbox_box" style="display:${
          item.state == 10 ? "block" : "none"
        };">
        任务暂未达成!
        </div>
        <button style="display:${
          item.state != 10 ? "block" : "none"
        };" class="recieve txtShadowRed font14" id="lingqu" data-id="${
          item[0].qi_num
        }" data-state="${item[0].state}">领取</button>
       </div>
     
      <div class="thingbox" style="display:${(item.state = 10
        ? "block"
        : "none")};">
   
      <p class="yellow"></p>
      <p class="orange"></p>
    </div>
      `;
      })
      .join(" ");
    ele.cabinet.innerHTML = newList;
    if (list.length == 0) {
      ele.cabinet.innerHTML = `<div class="zanwujilui"><span>暂无记录</span></div>`;
    }
    //
    // <div class="thingbox" style="display:${item.state==10?'block':'none'};">
    //   <div class="thingbox_box">任务暂未达成</div>
    // !渲染未领取
    let lingquAll = document.querySelectorAll("#lingqu");
    lingquAll.forEach((item) => {
      if (item.dataset.state == 2) {
        item.innerHTML = "领取";
        item.classList.remove("gray");
      } else if (item.dataset.state == 5) {
        // 已领取
        item.innerHTML = "已领取";
        item.classList.add("gray");
      } else if (item.dataset.state == 10) {
        item.innerHTML = "已结束";
        item.classList.add("gray");
      }
    });
    // !点击领取
    lingquAll.forEach((item) => {
      item.onclick = () => {
        claim(item.dataset.id);
      };
    });
    // !点击一键领取
    ele.yijian.onclick = () => {
      claim();
    };
    // !显示
    let xianshiAll = document.querySelectorAll("#xianshi");
    xianshiAll.forEach((item, ind) => {
      if (item.dataset.num == ind + 1) {
        let new1 = list[ind]
          .map((ite) => {
            return `<div class="thing"><div class="thingin" style="background-image: url(http://img.qxgs.cc/tiantianlaishouzu/img/reward/${
              ite.bx_type == 0 ? "3" : "1"
            }.png);">
            <img style="display:${
              ite.bx_type == 0 ? "block" : "none"
            };" src="http://img.qxgs.cc/tiantianlaishouzu/img/reward/6.png" alt="" />
            <img style="display:${
              ite.bx_type == 1 ? "block" : "none"
            };" src="http://img.qxgs.cc/tiantianlaishouzu/img/reward/7.png" alt="" />
            <img style="display:${
              ite.bx_type == 2 ? "block" : "none"
            };" src="http://img.qxgs.cc/tiantianlaishouzu/img/reward/8.png" alt="" />
            <img style="display:${
              ite.bx_type == 3 ? "block" : "none"
            };" src="http://img.qxgs.cc/tiantianlaishouzu/img/reward/9.png" alt="" />
            <img style="display:${
              ite.bx_type == 4 ? "block" : "none"
            };" src="http://img.qxgs.cc/tiantianlaishouzu/img/reward/9.png" alt="" />
            <span style="display:${ite.bx_type == 0 ? "block" : "none"};">${
              ite.get_money
            }</span>
            <span style="display:${
              ite.bx_type == 1 ? "block" : "none"
            };">楼主宝箱</span>
            <span style="display:${
              ite.bx_type == 2 ? "block" : "none"
            };">管家宝箱</span>
            <span style="display:${
              ite.bx_type == 3 ? "block" : "none"
            };">直管宝箱</span>
            <span style="display:${
              ite.bx_type == 4 ? "block" : "none"
            };">间管宝箱</span>
          </div>
          <img
            class="thingdimg"
            src="http://img.qxgs.cc/tiantianlaishouzu/img/reward/dizuo.png"
            alt=""
          /></div>`;
          })
          .join(" ");
        item.innerHTML = new1;
      }
    });
    // !宝箱
    //   let baoxiangAll = document.querySelectorAll("#baoxiang");
    //   baoxiangAll.forEach((item) => {
    //     if (item.dataset.type == 2) {
    //       // 没宝箱
    //       closeMdl(item);
    //     } else {
    //       if (item.dataset.h_id == 2 && item.dataset.type == 1) {
    //         item.innerHTML = `  <div class="thingin" style="background-image: url(http://img.qxgs.cc/tiantianlaishouzu/img/reward/2.png);">
    //   <img src="http://img.qxgs.cc/tiantianlaishouzu/img/reward/7.png" alt="" />
    //   <span>楼主宝箱</span>
    // </div>
    // <img
    //   class="thingdimg"
    //   src="http://img.qxgs.cc/tiantianlaishouzu/img/reward/dizuo.png"
    //   alt=""
    // />`;
    //       } else {
    //         if (item.dataset.type == 1 && item.dataset.num > 0) {
    //           item.innerHTML = `  <div class="thingin" style="background-image: url(http://img.qxgs.cc/tiantianlaishouzu/img/reward/1.png);">
    //           <img src="http://img.qxgs.cc/tiantianlaishouzu/img/reward/9.png" alt="" />
    //           <span>直管宝箱</span>
    //         </div>
    //         <img
    //           class="thingdimg"
    //           src="http://img.qxgs.cc/tiantianlaishouzu/img/reward/dizuo.png"
    //           alt=""
    //         />`;
    //         } else if (item.dataset.type == 3) {
    //           item.innerHTML = `  <div class="thingin" style="background-image: url(http://img.qxgs.cc/tiantianlaishouzu/img/reward/3.png);">
    //           <img src="http://img.qxgs.cc/tiantianlaishouzu/img/reward/8.png" alt="" />
    //           <span>管家宝箱</span>
    //         </div>
    //         <img
    //           class="thingdimg"
    //           src="http://img.qxgs.cc/tiantianlaishouzu/img/reward/dizuo.png"
    //           alt=""
    //         />`;
    //         } else if (item.dataset.type == 1 && item.dataset.num == 0) {
    //           item.innerHTML = `  <div class="thingin" style="background-image: url(http://img.qxgs.cc/tiantianlaishouzu/img/reward/1.png);">
    //           <img src="http://img.qxgs.cc/tiantianlaishouzu/img/reward/9.png" alt="" />
    //           <span>间管宝箱</span>
    //         </div>
    //         <img
    //           class="thingdimg"
    //           src="http://img.qxgs.cc/tiantianlaishouzu/img/reward/dizuo.png"
    //           alt=""
    //         />`;
    //         }
    //       }
    //     }
    //   });
  }
  // !领取记录
  function qiegejl() {
    let token = getCookie("token");
    ajax({
      method: "get",
      url: API_PATH.ZHAOJINBAO,
      header: {
        Authorization: token,
      },
      data: {
        page: page,
        // size: 1,
      },
      success: (res) => {
        if (res.code == 1) {
          // obj = res.data.detail;
          qgjlList = [...qgjlList, ...res.data.data];
          if (qgjlList.length === 0) {
            page = 0;
            ele.more.style.display = "none";
            ele.empty.style.display = "block";
          } else {
            jilulist();
            page++;
            if (page > res.data.last_page) {
              ele.more.innerText = "没有更多了";
              ele.more.onclick = null;
            } else {
              ele.more.innerText = "加载更多";
              ele.more.onclick = () => qiegejl(page);
            }
          }
        } else openTips(res.msg);
        jilulist();
      },
      error: (err) => {
        // openTips(err.msg);
      },
    });
  }
  function jilulist() {
    let newqgList = qgjlList
      .map((item) => {
        return `
      <div class="list_son">
              <div>${item.box_info.qi_num}</div>
              <div>${item.txt}</div>
              <div class="jinbi"><img src="${
                item.house_info
                  ? IMG_DOMAIN + item.house_info.logo
                  : "http://img.qxgs.cc/tiantianlaishouzu/img/public/jinbi.png"
              }" alt="">${item.money}</div>
            </div>
      `;
      })
      .join(" ");
    ele.list_jlu.innerHTML = newqgList;
  }
  // 领取奖励接口
  function claim(qi_num) {
	  qi_num = qi_num ? qi_num : '';
    let token = getCookie("token");
    ajax({
      method: "post",
      url: API_PATH.CLAIM,
      header: {
        Authorization: token,
      },
	  data: {
		  qi_num: qi_num
	  },
      success: (res) => {
        openTips(res.msg);
        if (res.code == 1) {
          asyncList();
          xuan(res.data);
          openMdl(ele.modal_getsuccess);
        } else {
        }
      },
      error: (err) => {
        // openTips(err.msg);
      },
    });
  }
  // 渲染奖励
  function xuan(list) {
    // console.log(list, "00000");
    let newList = list
      .map((item) => {
        if (item.info.b_type == 2) {
          return `   <div class="debox">
                    <div class="wups">
                      <img src="http://img.qxgs.cc/tiantianlaishouzu/img/home/jinbi3.png" alt="">
                    </div>
                    <p class="enums">${item.num}</p>
                  </div>`;
        } else {
          return `   <div class="debox">
                    <div class="wups">
                      <img src="${IMG_DOMAIN + item.info.logo}" alt="">
                    </div>
                    <p class="enums">${item.num}</p>
                  </div>`;
        }
      })
      .join(" ");
    ele.getsuccessList.innerHTML = newList;
  }
})();
